package com.czr.dao.service.impl;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.czr.dao.entity.CzrBusi;
import com.czr.dao.entity.CzrCompany;
import com.czr.dao.entity.CzrNews;
import com.czr.dao.entity.CzrUser;
import com.czr.dao.mapper.CzrBusiMapper;
import com.czr.dao.service.CzrBusiService;
import com.czr.dao.service.CzrUserService;
import com.czr.dto.CzrBusiParam;
import com.czr.dto.Search;
import com.czr.dto.StatusParam;
import com.czr.util.DateUtils;
import com.czr.util.PageUtil;
import com.czr.util.TencentCosUtil;
import com.czr.vo.CzrBusiGlVo;
import com.czr.vo.CzrBusiVo;
import com.czr.vo.CzrNewsVo;
import com.czr.vo.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * <p>
 * 名企推荐 服务实现类
 * </p>
 */
@Service
public class CzrBusiServiceImpl extends ServiceImpl<CzrBusiMapper, CzrBusi> implements CzrBusiService {

    @Autowired
    public CzrBusiMapper czrBusiMapper;

    @Autowired
    public CzrUserService czrUserService;

    @Override
    public Result<Object> list(Search search) throws Exception {
        Page<CzrBusi> page = new Page<>();
        page.setSize(search.getSize());
        page.setCurrent(search.getCurrent());
        LambdaQueryWrapper<CzrBusi> ew = new LambdaQueryWrapper<>();
        if (com.baomidou.mybatisplus.core.toolkit.StringUtils.isNotBlank(search.getKeyword())) {
            ew.like(CzrBusi::getBusiName, search.getKeyword()).or().like(CzrBusi::getBusiCompanyBrief, search.getKeyword());

        }
        ew.eq(CzrBusi::getBusiStatus, 1);
        ew.orderByDesc(CzrBusi::getInsertTime);
        page = this.getBaseMapper().selectPage(page, ew);
        JSONArray array = new JSONArray();
        for (CzrBusi czrBusi : page.getRecords()) {
            CzrBusiVo czrBusiVo = new CzrBusiVo();
            BeanUtils.copyProperties(czrBusi, czrBusiVo);
            array.add(czrBusiVo);
        }
        JSONObject object = PageUtil.getPage(array, page);
        return Result.data(object);
    }

    @Override
    public Result<Map<String, Object>> add(CzrBusiParam czrBusiParam, Integer userId) throws Exception {

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //验证账号是否存在
        Map<String, Object> map = czrBusiMapper.getCompanyName(czrBusiParam.getBusiCompanyName());
        if (!StringUtils.isEmpty(map)) {
            return Result.success(201, "该企业已申请，请勿重复申请");
        }
        CzrBusi czrBusi = new CzrBusi();
        BeanUtils.copyProperties(czrBusiParam, czrBusi);
        czrBusi.setUserId(userId);
        czrBusi.setBusiStatus(0);
        czrBusi.setInsertTime(format.format(new Date()));
        boolean resultBoolean = czrBusi.insert();
        if (resultBoolean) {
            return Result.success(200, "提交成功，等待审核！");
        } else {
            return Result.fail("提交失败！");
        }
    }

    @Override
    public Result<Map<String, Object>> update(CzrBusi czrBusi) throws Exception {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //验证名企信息是否存在
        CzrBusi map = this.getById(czrBusi.getId());
        if (StringUtils.isEmpty(map)) {
            return Result.fail("名企ID有误，请检查！");
        }
        czrBusi.setVerifyTime(format.format(new Date()));
        boolean resultBoolean = czrBusi.updateById();
        if (resultBoolean) {
            return Result.success(200, "审核成功！");
        } else {
            return Result.fail("审核失败！");
        }
    }

    @Override
    public Result<Object> glList(StatusParam statusParam) throws Exception {
        TencentCosUtil tencentCosUtil=new TencentCosUtil();
        Page<CzrBusi> page = new Page<>();
        page.setSize(statusParam.getSize());
        page.setCurrent(statusParam.getCurrent());
        LambdaQueryWrapper<CzrBusi> ew = new LambdaQueryWrapper<>();
        if (com.baomidou.mybatisplus.core.toolkit.StringUtils.isNotBlank(statusParam.getKeyword())) {
            ew.like(CzrBusi::getBusiName, statusParam.getKeyword()).or().like(CzrBusi::getBusiCompanyBrief, statusParam.getKeyword());

        }
        if (!StringUtils.isEmpty(statusParam.getStatus())) {
            ew.eq(CzrBusi::getBusiStatus, statusParam.getStatus());
        }
        ew.orderByAsc(CzrBusi::getInsertTime);
        page = this.getBaseMapper().selectPage(page, ew);
        JSONArray array = new JSONArray();
        for (CzrBusi czrBusi : page.getRecords()) {
            CzrBusiGlVo czrBusiGlVo = new CzrBusiGlVo();
            BeanUtils.copyProperties(czrBusi, czrBusiGlVo);
            //查询用户表
            CzrUser czrUser = czrUserService.getById(czrBusi.getUserId());
            if (!StringUtils.isEmpty(czrUser)) {
                czrBusiGlVo.setUserName(czrUser.getUsername());
            }
            if (!StringUtils.isEmpty(czrBusiGlVo.getBusiCompanyLogo())) {
                czrBusiGlVo.setBusiCompanyLogo(tencentCosUtil.imgFile_wx(czrBusiGlVo.getBusiCompanyLogo()));
            }
            array.add(czrBusiGlVo);
        }
        JSONObject object = PageUtil.getPage(array, page);
        return Result.data(object);
    }

    @Override
    public Result<Map<String, Object>> delBusi(Integer busiId, JSONObject cacheToken) throws Exception {

        if(!StringUtils.isEmpty(cacheToken) && cacheToken.getInteger("user_type")!=1){
            return Result.success(201,"非管理员账号，无法删除申请！");
        }
        CzrBusi czrBusi=czrBusiMapper.selectById(busiId);
        if(StringUtils.isEmpty(czrBusi)){
            return Result.success(201,"无效ID！");
        }
        boolean resultBoolean=czrBusi.deleteById();
        if(resultBoolean){
            return Result.success(200,"删除成功");
        }else{
            return Result.success(201,"删除失败！");
        }
    }
}
